package one;

import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;

import java.io.IOException;
import java.io.StringReader;

/**
 * @author wangpp
 */
public class JsonReaderExample {
    public static void main(String[] args) throws IOException {
        iteratingJsonReader();
    }

    private static void iteratingJsonReader() throws IOException {
        String json = "{\"brand\" : \"Toyota\", \"doors\" : 5}";
        JsonReader reader = new JsonReader(new StringReader(json));
        while (reader.hasNext()) {
            JsonToken token = reader.peek();
            System.out.println(token);

            if (JsonToken.BEGIN_OBJECT.equals(token)) {
                reader.beginObject();
            } else if (JsonToken.NAME.equals(token)) {

                String name = reader.nextName();
                System.out.println(name);

            } else if (JsonToken.STRING.equals(token)) {

                String s = reader.nextString();
                System.out.println(s);

            } else if (JsonToken.NUMBER.equals(token)) {
                long l = reader.nextLong();
                System.out.println(l);
            }
        }
    }
}
